import com.google.common.util.concurrent.ThreadFactoryBuilder;
import org.junit.Test;

import java.util.concurrent.*;

/**
 * @Description TODO
 * @Author 作者： cm
 * @DATE 创建时间： 2021/9/2 9:58
 * @Version 版本：1.0
 */
public class test {
    //引入依赖包，创建线程池
    private ThreadFactory namedThreadFactory = new ThreadFactoryBuilder().setNameFormat("thread-DB-runner-%d").build();
    private ExecutorService taskExe = new ThreadPoolExecutor(10, 20, 200L,
            TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>());

    @Test
    public void test1() throws InterruptedException {
        for (int i = 0; i < 5; i++) {
            String lineName = "qst"+i;
            taskExe.submit(() -> Thread.currentThread().setName("DB-"+ lineName));
        }
        ThreadGroup currentGroup = Thread.currentThread().getThreadGroup();
        int noThreads = currentGroup.activeCount();
        Thread[] lstThreads = new Thread[noThreads];
        currentGroup.enumerate(lstThreads);
        for (int i = 0; i < noThreads; i++) {
            System.out.println("线程号：" + i + " = " + lstThreads[i].getName());

        }
    }

    @Test
    public void test() throws InterruptedException {
        CountDownLatch countDownLatch = new CountDownLatch(1);
        for (int i = 0; i < 5; i++) {
            new Thread(() -> {
                try {
                    //准备完毕……运动员都阻塞在这，等待号令
                    countDownLatch.await();
                    String parter = "【" + Thread.currentThread().getName() + "】";
                    System.out.println(parter + "开始执行……");
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }).start();
        }

        Thread.sleep(2000);// 裁判准备发令
        countDownLatch.countDown();// 发令枪：执行发令
    }
}
